﻿using Api.Application.Command.Report;
using Api.DTO;
using Domain;
using ErrorCode;
using Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Api.Application.Handler.Report
{
    public class GetLeaveDetailHandler : IRequestHandler<GetLeaveDetailCommand, ApiResult<List<LeaveRecordDTO>>>
    {
        private readonly IBase<UserModel> userRepo;
        private readonly IBase<LeaveModel> leaveRepo;

        public GetLeaveDetailHandler(IBase<UserModel>userRepo,IBase<LeaveModel> leaveRepo)
        {
            this.userRepo = userRepo;
            this.leaveRepo = leaveRepo;
        }
        public  async Task<ApiResult<List<LeaveRecordDTO>>> Handle(GetLeaveDetailCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<LeaveRecordDTO>> result=new ApiResult<List<LeaveRecordDTO>>();
            var users =await userRepo.GetValue(request.UserId);
            if (users == null)
            {
                result.Msg = "用户不存在";
                result.Code = ApiEnum.Fail;
                return await Task.FromResult(result);
            }
            var leave = await leaveRepo.GetAll().Where(x => x.UserId == request.UserId && x.LeaveStartTime.Year==DateTime.Now.Year&&x.LeaveStartTime.Month == request.Month).Select(x => new LeaveRecordDTO
            {
                LeaveEndTime = x.LeaveEndTime,
                LeaveStartTime = x.LeaveStartTime,
                LeaveReason = x.LeaveReason,
                LeaveZong = x.LeaveZong,
                Month=request.Month,
                UserId=request.UserId,
                UserName=x.UserName,
            }).ToListAsync();
            result.Code = ApiEnum.OK;
            result.Data= leave;
            result.Msg = "查询名称成功";
            return await Task.FromResult(result);
        }
    }
}
